const app = getApp();
var api = require('../../../api.js');

Page({
  data: {
    mouth: "",
    rewardList: [],
    date: '2018-06',
    year: '',
    month: '',
    endDate: '2018-06',
    zero: "您还没有每月奖励，请加油！",
    judge: 0,
    pageCount: 1, //加载更多的分页数
    allPage: 0,  //总分页
  },
  onLoad: function (options) {
    app.bind();
    this.newDate();
  },
  onReady: function () {
    this.getIdealMoneyMouth();
  },
  onShow: function () {

  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

    // app.bind();
    // this.newDate();
    // this.getIdealMoneyMouth();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {

    var that = this;
    // 分页数
    var allPage = that.data.allPage;
    var pageCount = that.data.pageCount;
    pageCount ++;
    that.setData({
      pageCount: pageCount,
    })

    if (pageCount <= allPage) {
      that.getIdealMoneyMouth();
    } else {
      wx.showToast({
        title: '全部加载完成，不能加载更多',
        icon: 'none',
      })
    }

  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
  },
  // 获取基本信息
  getIdealMoneyMouth: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var url = api.user.idealReward;
    var pageCount = that.data.pageCount;
    url = url + "&page=" + pageCount;

    wx.request({
      url: url,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        console.log(e);
        var allPage = e.data.data.page_count;
        var list = e.data.data.list;
        var listOrigin = that.data.rewardList;
        var listLast = listOrigin.concat(list);

        if (e.data.code == 0) {

          if (e.data.data.row_count == 0) {
            that.setData({
              judge: 1,
            }) 
          }
          
          that.setData({
            mouth: e.data.month_integral,
            allPage: allPage,
            rewardList: listLast,
          })
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },

  //  获取当前时间
  newDate: function (e) {
    var that = this;
    var myDate = new Date();
    var fullYear = myDate.getFullYear();
    var month = myDate.getMonth();
    month ++;
    if (month < 10) {
      month = "0" + month;
    }
    var date = fullYear + "-" + month;
    console.log(date);
    that.setData({
      date: date,
      endDate: date,
      year: fullYear,
      month: month,
    })
  },

  // 改变顶部时间
  bindDateChange: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
})